package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.annotation.LogAction;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.Dictionary;
import com.jokerpan.project.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("dictionary")
@Api("字典管理Api")
public class DictionaryController {

    @Resource
    DictionaryService dictionaryService;

    @GetMapping("/queryList")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有字典")
    @PreAuthorize("hasPermission('','admin:list')")
    public ListResult<Object> queryList(Dictionary dictionary) {
        return ResultBuilder.buildListSuccess(this.dictionaryService.queryList(dictionary));
    }

    @PostMapping("/add")
    @ApiOperation("新增字典")
    @AccessLimit(seconds = 6, maxCount = 5)
    @LogAction("字典管理,新增字典数据,新增字典,新增")
    @PreAuthorize("hasPermission('','admin:update')")
    public ActionResult addRole(@RequestBody Dictionary dictionary) throws Exception {
        if (this.dictionaryService.add(dictionary)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

    @GetMapping("/queryByCode")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("根据编码查询字典")
    public ListResult<Object> queryByCode(@RequestParam("code") String code){
        return ResultBuilder.buildListSuccess(this.dictionaryService.queryByCode(code));
    }

    @PostMapping("/update")
    @ApiOperation("修改字典")
    @AccessLimit(seconds = 6, maxCount = 5)
    @LogAction("字典管理,修改字典数据,修改字典,修改")
    @PreAuthorize("hasPermission('','admin:update')")
    public ActionResult update(@RequestBody Dictionary dictionary){
        return this.dictionaryService.update(dictionary);
    }

    @GetMapping("/delete")
    @ApiOperation("删除字典")
    @AccessLimit(seconds = 6, maxCount = 5)
    @LogAction("字典管理,删除字典数据,删除字典,删除")
    @PreAuthorize("hasPermission('','admin:delete')")
    public ActionResult delete(@RequestParam("id") Integer id){
        return this.dictionaryService.deleteById(id);
    }

}
